package cn.com.KnowNews.dao.daoImpl;

import cn.com.KnowNews.dao.IAdminDao;
import cn.com.KnowNews.entity.Admin;
import cn.com.KnowNews.utils.DBConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AdminDaoImpl implements IAdminDao {

    private Connection conn = null;
    private PreparedStatement ps;

    public Admin checkAdmin(Admin admin) throws Exception {

        conn= DBConn.getConnection();
        String sql = "select * from t_admin where username=? and password=?";
        ps=conn.prepareStatement(sql);
        ps.setString(1,admin.getUsername());
        ps.setString(2,admin.getPassword());
        ResultSet rs = ps.executeQuery();
        Admin resultAdmin = null;
        if(rs.next()){
            resultAdmin=new Admin();
            resultAdmin.setUsername(rs.getString("username"));
            resultAdmin.setPassword(rs.getString("password"));
        }
        DBConn.release(rs,ps,conn);
        return resultAdmin;
    }

    @Override
    public Boolean addAdmin(Admin admin) throws Exception {
        conn = DBConn.getConnection();
        String sql = "insert into t_admin (username,password) values (?,?)";
        ps=conn.prepareStatement(sql);
        ps.setString(1,admin.getUsername());
        ps.setString(2,admin.getPassword());
        int resultAdmin = ps.executeUpdate();
        DBConn.release(ps,conn);
        if(resultAdmin>0){
            return true;
        }else{
            return false;
        }
    }
}
